﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business;
using Model;
using BarcodeLib;
using System.IO;

namespace MvcAppWeb.Controllers
{
    public class PrintGoodsBatchCodeController : Controller
    {
        //
        // GET: /PrintGoodsBatchCode/

        public ActionResult PrintGoodsBatchCode()
        {

            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);
            }
        }

        [HttpPost]
        public string checkGoodsExistByBarCode()
        {
            string sjson = string.Empty;
            string code = Request["txtBarCode"];

            BLocation nbl = new BLocation();
            Goods check = nbl.CheckGoodsByBarCode(code);
            if (check != null)
            {
                sjson = "{success:true}";
            }
            else
            {
                sjson = "{success:false,msg:'该商品条码不存在！'}";
            }
            return sjson;
        }
        [HttpPost]
        public string CreateImage(string BarCode, string ProductionDate, string ExpiryDays)
        {
            string sjson = string.Empty;
            string code = string.Empty;
            string KeepDay = string.Empty;
            BLocation nbl = new BLocation();
            Goods checkgoods = nbl.CheckGoodsByBarCode(BarCode);
            if (checkgoods != null)
            {
                if (string.IsNullOrEmpty(ExpiryDays) && checkgoods.KEEPDAYS <= 0)
                {
                    sjson = "{success:false,msg:'没有保质期信息！'}";
                    return sjson;
                }
                if (checkgoods.KEEPDAYS > 0)
                    KeepDay = checkgoods.KEEPDAYS.ToString("000");
                else
                {
                    int _ExpiryDays = 0;
                    if (int.TryParse(ExpiryDays, out _ExpiryDays))
                        KeepDay = _ExpiryDays.ToString("000");
                    else
                    {
                        sjson = "{success:false,msg:' 保质期信息有误！'}";
                        return sjson;
                    }
                }
            }
            else
            {
                sjson = "{success:false,msg:'该商品条码不存在！'}";
                return sjson;
            }
            code = BarCode + Convert.ToDateTime(ProductionDate).ToString("yyyyMMdd").Substring(3,5) + KeepDay;
            System.Drawing.Image image;
            int width = 300, height = 100;
            string filename = NewTimeStamp("BarCode");
            string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + "upload/" + filename;
            if (System.IO.File.Exists(fileSavePath))
                System.IO.File.Delete(fileSavePath);
            GetBarcode(height, width, BarcodeLib.TYPE.CODE128, code, out image, fileSavePath);
            sjson = "{success:true,msg:'/upload/" + filename + "'}";
            return sjson;
        }

        public static void GetBarcode(int height, int width, BarcodeLib.TYPE type, string code, out System.Drawing.Image image, string fileSaveUrl)
        {
            try
            {
                image = null;
                BarcodeLib.Barcode b = new BarcodeLib.Barcode();
                b.BackColor = System.Drawing.Color.White;//图片背景颜色  
                b.ForeColor = System.Drawing.Color.Black;//条码颜色  
                b.IncludeLabel = true;
                b.Alignment = BarcodeLib.AlignmentPositions.LEFT;
                b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
                b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;//图片格式  
                System.Drawing.Font font = new System.Drawing.Font("verdana", 10f);//字体设置  
                b.LabelFont = font;
                b.Height = height;//图片高度设置(px单位)  
                b.Width = width;//图片宽度设置(px单位)  
                image = b.Encode(type, code);//生成图片  
                image.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                image = null;
            }
        }

        public string NewTimeStamp(string strFileName)
        {

            //生成文件名: 当前年月日小时分钟秒+ 随机数
            Random rd = new Random(int.Parse(DateTime.Now.ToString("MMddhhmmss")));
            strFileName += DateTime.Now.ToString("yyyyMMdd")
                + DateTime.Now.Hour
                + DateTime.Now.Minute
                + DateTime.Now.Second
                + rd.Next(999999).ToString()
                + ".jpg";
            return strFileName;
        }
    }
}
